package com.demo2do.fms.admin.web.controller.query;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.query.DriverQuery;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.service.supplier.DriverService;
import com.demo2do.fms.entity.driver.Driver;
import com.demo2do.fms.entity.driver.DriverAuditStatus;

/**
 * 司机辅助查询
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/drivers")
public class DriverQueryController {
    
    @Autowired
    private DataService<Driver> dataService;
    
    @Autowired
    private DriverService driverService;
    
    /**
     * 司机失去焦点查询
     * 
     * @param code
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET, params = "code", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onQuery(@RequestParam("code") String code,
                                            @RequestParam(name = "orgIds", required = false) Long supplierId) {
        
        HttpResult httpResult = new HttpResult();
        
        Driver driver = driverService.load(supplierId, code, DriverAuditStatus.QUALIFIED);
        
        if(driver != null) {
            
            httpResult.success().data("id", driver.getId())
                                .data("code", driver.getCode())
                                .data("name", driver.getName());
            
            return httpResult;
            
        }
       
        return httpResult.fail("400").message("查询失败");
        
    }
    
    /**
     * 弹窗查询
     * 
     * @return
     */
    @RequestMapping("/query")
    public String modalQuery() {
        return "query/driver-query-modal";
    }
    
    /**
     * 
     * @param driverQuery
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "query" , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onModalQuery(DriverQuery driverQuery,
                                                 HttpServletRequest request,
                                                 Page page) {
        
        HttpResult httpResult = new HttpResult();
        
        int totals = dataService.count(Driver.class, driverQuery.getFilter());
        if(totals > 0){
            
            List<Driver> drivers = dataService.search(Driver.class, driverQuery, page.with(totals));
            
            httpResult.success().data("list", drivers)
                                .data("page", page)
                                .data("queryString", WebUtils.extractQueryString(request, "query", "page"));
        } else {
            httpResult.fail("500").message("查询结果为空");
        }
        
        return httpResult;
        
    }
    
   
    
}
